package com.hanbang.baseproject.pressenter.othre;

import android.app.Activity;

import com.hanbang.baseproject.code.base.presenter.BasePresenter;
import com.hanbang.baseproject.mode.httpresponse.BaseHttpResponse;
import com.hanbang.baseproject.mode.javabean.base.UserData;
import com.hanbang.baseproject.utils.http.GsonHelper;
import com.hanbang.baseproject.utils.http.httpquest.HttpCallBack;
import com.hanbang.baseproject.utils.other.LogUtils;
import com.hanbang.baseproject.utils.ui.SnackbarUtil;
import com.hanbang.baseproject.view.login.iview.IloginView;

import org.json.JSONObject;

import okhttp3.ResponseBody;

/**
 * Created by yang on 2016/8/17.
 */
public class LoginPresenter extends BasePresenter<IloginView> {

    public void getHttpData(final Activity activity, String phone) {

        HttpCallBack.Buider buider = new HttpCallBack.Buider(this).setShowLoadding(true).setLoadingAndRetryManager(mvpView.getLoadingAndRetryManager());

        HttpCallBack httpCallBack = new HttpCallBack<BaseHttpResponse>(buider) {

            @Override
            public void onSuccess(BaseHttpResponse result) {
                super.onSuccess(result);

                //进行data处理
                if (result.isSucceed()) {
                    mvpView.clearData();
                    mvpView.isVerification(true);
                    SnackbarUtil.showLong(activity, result.getMsg(), SnackbarUtil.Info).show();
                } else {
                    mvpView.isVerification(false);
                    SnackbarUtil.showLong(activity, result.getMsg(), SnackbarUtil.Info).show();
                }

            }
        };

        //  mvpView.addSubscription(HttpRequest.getLoginVerificationCode(httpCallBack, phone));
    }


    public void login(final Activity context, String telphone, String code) {

        HttpCallBack.Buider buider = new HttpCallBack.Buider(this).setShowLoadding(false).setLoadingAndRetryManager(mvpView.getLoadingAndRetryManager());

        HttpCallBack httpCallBack = new HttpCallBack<ResponseBody>(buider) {

            @Override
            public void onSuccess(ResponseBody result) {
                super.onSuccess(result);

                try {
                    JSONObject jsonObject = new JSONObject(result.string().toString());
                    int status = jsonObject.getInt("Result");

                    if (status == 0) {
                        UserData userData = GsonHelper.getGson().fromJson(jsonObject.getJSONObject("model").toString(), UserData.class);
                        mvpView.login(userData);

                        SnackbarUtil.showLong(context, "登录成功", SnackbarUtil.Info).show();
                    } else {
                        SnackbarUtil.showLong(context, jsonObject.getString("Msg"), SnackbarUtil.Info).show();
                    }


                    LogUtils.e(result.string());

                } catch (Exception e) {
                    e.printStackTrace();
                }


                mvpView.clearData();

            }
        };


        // mvpView.addSubscription(HttpRequest.login(httpCallBack, telphone, code));
    }


}
